/**
 * @Author: LiMeng
 * @Date: 2017/4/26
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    YEARS: []
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorAuditName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
};
var getSelectedUser = function (data) {
    data = data[0];

    $('#evaluatorName').textbox('setValue', data.realName);
    $('#evaluatorId').val(data.id);
};
var openSelectUser = function (title) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml'
    });
};
var toggleComboBox = function () {
    var $isExposure = $('#isExposure');
    var $isQualified = $('#isQualified');
    var $exposureStartDate = $('#exposureStartDate');
    var $exposureEndDate = $('#exposureEndDate');
    var $warp_is_exposure = $isExposure.parents('.form_one');
    var $warp_exposure_start_date = $exposureStartDate.parents('.form_one');
    var $warp_exposure_end_date = $exposureEndDate.parents('.form_one');

    $exposureStartDate.datebox('disable');
    $exposureEndDate.datebox('disable');

    $isQualified.combobox({
        onChange: function () {
            if ($(this).val().toString() === '0') {
                $warp_is_exposure.removeClass('hide');
            } else {
                $warp_is_exposure.addClass('hide');
                $warp_exposure_start_date.addClass('hide');
                $warp_exposure_end_date.addClass('hide');
                $exposureStartDate.datebox('disable');
                $exposureEndDate.datebox('disable');
                $isExposure.combobox('setValue', 0);
            }
        }
    });
    $isExposure.combobox({
        onChange: function () {
            if ($(this).val().toString() === '1') {
                $warp_exposure_start_date.removeClass('hide');
                $warp_exposure_end_date.removeClass('hide');
                $exposureStartDate.datebox('enable');
                $exposureEndDate.datebox('enable');
            } else {
                $warp_exposure_start_date.addClass('hide');
                $warp_exposure_end_date.addClass('hide');
                $exposureStartDate.datebox('disable');
                $exposureEndDate.datebox('disable');
            }
        }
    });
};
var getRecentTwentyYearsComboList = function () {
    var thisYear = new Date().getFullYear();
    for (var i = thisYear + 10; i > thisYear - 10; i--) {
        GLOBAL.YEARS.push({
            value: i,
            text: i
        });
    }
};
$(function () {
    getRecentTwentyYearsComboList();

    $('#contractorAuditName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                var me = this;
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['700px', '400px'],
                    content: ROOT_PATH + '/contractor/contractorAudit/auditedList.shtml',
                    callBack: getSelectedContractorAudit
                });
            }
        }],
        editable: false
    });

    $('#year').combobox({
        data: GLOBAL.YEARS,
        value: new Date().getFullYear(),
        editable: false,
        required: true
    });

    $('#evaluatorName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择评价人', getSelectedUser);
            }
        }],
        editable: false
    });

    toggleComboBox();

    $('#btn_save').on('click', function () {
        $.messager.progress();	// 显示进度条

        $('#form1').form('submit', {
            url: ROOT_PATH + '/contractor/annualEvaluation/add.shtml',
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $.getIframeWindow().$('#dg1').datagrid('load');
                        var id = result.data.id;
                        window.location.href = ROOT_PATH + '/contractor/annualEvaluation/edit.shtml?'
                            + 'id=' + id
                            + '&resourceId=' + $.getQueryParam('resourceId')
                            + '&state=edit';
                    });
                } else {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {icon: 2});
                }
            }
        });
    });
});